package com.shenda.api.dao;

import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.shenda.api.entity.Log;
import com.swak.entity.Page;
import com.swak.entity.Parameters;
import com.swak.persistence.BaseJdbcDao;
import com.swak.persistence.QueryCondition;
import com.swak.utils.Maps;

/**
 * 数据库存储操作
 * 
 * @author lifeng
 */
@Repository
public class LogDao extends BaseJdbcDao {

	/**
	 * 通过ID获取
	 * 
	 * @param id
	 * @return
	 */
	public Log get(Long id) {
		Map<String, Object> param = Maps.newHashMap();
		param.put("id", id);
		return this.get("SELECT ID, COMM, OPSCMD, OPSTIME, OPSCONTENT, GYRO, OPSTYPE FROM LOG WHERE ID = :id", param,
				this.getRowMapper());
	}

	/**
	 * 插入
	 * 
	 * @param id
	 * @return
	 */
	public void insert(Log gyro) {
		gyro.prePersist();
		Map<String, Object> param = this.BeantoMap(gyro);
		this.insert(
				"INSERT INTO LOG(ID, COMM, OPSCMD, OPSTIME, OPSCONTENT, GYRO, OPSTYPE) VALUES(:id, :comm, :opsCmd, :opsTime, :opsContent, :gyro, :opsType)",
				param);
	}

	/**
	 * 分页查询
	 * 
	 * @param gyro
	 * @param param
	 * @return
	 */
	public Page page(QueryCondition query, Parameters param) {
		String sql = "SELECT ID, COMM, OPSCMD, OPSTIME, OPSCONTENT, GYRO, OPSTYPE FROM LOG";
		return this.page(sql, query, param, this.getRowMapper());
	}

	/**
	 * 查询数量
	 * 
	 * @param query
	 * @return
	 */
	public Integer count(QueryCondition query) {
		String sql = "SELECT ID, COMM, OPSCMD, OPSTIME, OPSCONTENT, GYRO, OPSTYPE FROM LOG";
		return this.count(sql, query);
	}

	/**
	 * 数据转换
	 * 
	 * @return
	 */
	private RowMapper<Log> getRowMapper() {
		return (rs, num) -> {
			Log gyro = new Log();
			gyro.setId(rs.getLong("ID"));
			gyro.setComm(rs.getString("COMM"));
			gyro.setGyro(rs.getString("GYRO"));
			gyro.setOpsCmd(rs.getString("OPSCMD"));
			gyro.setOpsContent(rs.getString("OPSCONTENT"));
			gyro.setOpsTime(rs.getString("OPSTIME"));
			gyro.setOpsType(rs.getString("OPSTYPE"));
			return gyro;
		};
	}
}
